package net.projecteuler.prob;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * High Card: Highest value card.
 * One Pair: Two cards of the same value.
 * Two Pairs: Two different pairs.
 * Three of a Kind: Three cards of the same value.
 * Straight: All cards are consecutive values.
 * Flush: All cards of the same suit.
 * Full House: Three of a kind and a pair.
 * Four of a Kind: Four cards of the same value.
 * Straight Flush: All cards are consecutive values of same suit.
 * Royal Flush: Ten, Jack, Queen, King, Ace, in same suit.
 * */

public class Problem54 implements Runnable {
	
	private static final int RANK_HighCard = 0;
	private static final int RANK_OnePair = 1;
	private static final int RANK_ThreeOfAKind = 2;
	private static final int RANK_Straight = 3;
	private static final int RANK_Flush = 4;
	private static final int RANK_FullHouse = 5;
	private static final int RANK_FourOfAKind = 6;
	private static final int RANK_StraightFlush = 7;
	private static final int RANK_RoyalFlush = 8;
	
	private static final String txtURL = "http://projecteuler.net/project/poker.txt";
	
	private BufferedReader reader = null;
	private int[][] player1 = null;
	private int[][] player2 = null;
	
	private Comparator<int[]> cardComp = new Comparator<int[]>() {
		
		@Override
		public int compare(int[] o1, int[] o2) {
			if(o1[0] > o2[0])
				return 1;
			else if(o1[0] < o2[0])
				return -1;
			else{
				if(o1[1] > o2[1])
					return 1;
				else if(o1[1] < o2[1])
					return -1;
			}
			return 0;
		}
	};
	
	@Override
	public void run() {		
		if(!openFile(txtURL)){
			System.out.println("file open failed");
			return;
		}
		
		int cntP1wins = 0;
		while(getNextHands()){
			int[][] score1 = getScores(player1);
			int[][] score2 = getScores(player2);
		}
		
		return;
	}
	
	private boolean getNextHands(){
		try {
			String str = reader.readLine();
			if(str == null)
				return false;
			else{
				String[] cards = str.split(" ");
				player1 = new int[5][];
				player2 = new int[5][];
				for(int i = 0; i < 5; i++){
					player1[i] = getCard(cards[i]);
					player2[i] = getCard(cards[i + 5]);
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return true;
	}
	
	private int[] getCard(String str){
		int[] card = new int[2];
		char numCh = str.charAt(0);
		char suitCh = str.charAt(1);
		
		if(numCh >= '2' && numCh <= '9')
			card[0] = numCh - '0';
		else if(numCh == 'A')
			card[0] = 1;
		else if(numCh == 'T')
			card[0] = 10;
		else if(numCh == 'J')
			card[0] = 11;
		else if(numCh == 'Q')
			card[0] = 12;
		else if(numCh == 'K')
			card[0] = 13;
		
		if(suitCh == 'S')
			card[1] = 1;
		else if(suitCh == 'D')
			card[1] = 2;
		else if(suitCh == 'H')
			card[1] = 3;
		else if(suitCh == 'C')
			card[1] = 4;
		
		return card;
	}
	
	private boolean openFile(String txtURL){
		try {
			URL url = new URL(txtURL);
			InputStreamReader in = new InputStreamReader(url.openStream());
			reader = new BufferedReader(in);
		} catch (MalformedURLException e) {
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		
		return true;
	}
		
	private int[][] getScores(int[][] hand){
		// Sort card
		List<int[]> score = new ArrayList<int[]>();
		
		// RANK_RoyalFlush
		Arrays.sort(hand, cardComp);
		
		
		return null;
	}

	private boolean chkRoyalFlush(int[][] hand){		
		int suit = hand[0][1];
		for(int i = 1; i < 5; i++)
			if(suit != hand[i][1])
				return false;
		
		if(hand[0][0] != 1 || hand[1][0] != 10 || hand[2][0] != 11 || hand[3][0] != 12 || hand[4][0] != 13)
			return false;
		
		hand = new int[][]{};		
		return true;
	}
	
}
